package com.yinhe.music.yhmusic.login.validcode;

import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.IBaseModel;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.http.StatusCode;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;

public class ValidCodePresenter extends BasePresenter<IBaseModel, ValidCodeContract.IValidCodeView> implements ValidCodeContract.IValidCodePresenter {
    @Override
    public void getValidCode(String phone) {
        if (getView() == null) {
            return;
        }

        getView().showLoading();

        addSubscription(mModel.getValidCode(IConstants.PASSWORD, phone)
                .compose(schedulerProvider.applySchedulers())
                .subscribe(baseInfoResponse -> {
                    if (baseInfoResponse.getCode() == StatusCode.CHANGE_DATA) {
                        getView().getCodeUI();
                    }
                    getView().showToast(baseInfoResponse.getMsg());
                    getView().hideLoading();
                }, throwable -> {
                    getView().hideLoading();
                    getView().showMessage(throwable);
                }));
    }

    @Override
    public void checkValidCode(String phone, String validCode) {
        if (getView() == null) {
            return;
        }

        addSubscription(mModel.checkValidCode(validCode, phone)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(validCodeInfo -> {
                    getView().startNext(validCodeInfo);
                }, throwable -> {
                    getView().showMessage(throwable);
                }));
    }
}
